package com.cx.controller;

import com.cx.common.ApiReturn;
import com.cx.pojo.Engine;
import com.cx.service.EngineService;
import com.github.pagehelper.PageInfo;
import com.sun.xml.internal.ws.server.provider.AsyncProviderInvokerTube;
import org.omg.CORBA.PUBLIC_MEMBER;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("engine")
@CrossOrigin//跨域
public class EngineController {

    @Autowired
    EngineService engineService;

    //添加发动机
    @RequestMapping("insertEngine")
    @ResponseBody
    public ApiReturn insertEngine(Engine engine){
        int rows = engineService.insert(engine);
        if (rows<1){
            return ApiReturn.error("添加失败");
        }
        return  ApiReturn.success("添加成功");
    }

    //修改发动机
    @RequestMapping("updateEngine")
    @ResponseBody
    public ApiReturn updateEngine(Engine engine){
        int rows = engineService.updateByPrimaryKey(engine);
        if (rows<1){
            return ApiReturn.error("修改失败");
        }
        return ApiReturn.success("修改成功");
    }

    //删除发动机
    @RequestMapping("deleteEngine")
    @ResponseBody
    public ApiReturn deleteEngine(Integer eId) {
        int rows = engineService.deleteByPrimaryKey(eId);
        if (rows < 1) {
            return ApiReturn.error("删除失败");
        }
        return ApiReturn.success("删除成功");
    }

    //根据id查询发动机
    @RequestMapping("selectEngineById")
    @ResponseBody
    public ApiReturn selectEngineById(Integer eId) {
        Engine engineInfo = engineService.selectByPrimaryKey(eId);
        if (engineInfo != null) {
            return ApiReturn.success("发动机数据详情",engineInfo);
        }
        return ApiReturn.error("未获取该发动机数据详情");
    }

    //查询所有发动机
    @RequestMapping("selectAllEngine")
    @ResponseBody
    public ApiReturn selectAllEngine(Engine engine) {
        PageInfo<Engine> page = engineService.selectAllEngine(engine);
        if (page.getList() != null) {
            return ApiReturn.success("所有发动机数据",page);
        }
        return ApiReturn.error("无数据");
    }
}
